package com.google.code.p.paintboard.proportions;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;


@SuppressWarnings("serial")
public class CustomPanel extends JPanel {
	int linesX = 10;
	int linesY = 10;
	private BufferedImage img;
	private double boardWidth;
	private double boardHeight;
	private double originalWidth;
	private double originalHeight;
	private int shiftX;
	private int shiftY;
	private int scale;
	
	
	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(Color.white);
		g.fillRect(0, 0, getWidth(), getHeight());
		
		// compute scaled image
		int drawWidth = img.getWidth();
		int drawHeight = img.getHeight();
		if(img.getWidth() > img.getHeight()) {
			drawWidth += scale;
			drawHeight = img.getHeight() * drawWidth / img.getHeight();
		}
		else {
			drawHeight += scale;
			drawWidth = img.getWidth() * drawHeight / img.getWidth();
		}
		g.drawImage(img, 0-shiftX, 0-shiftY, drawWidth, drawHeight, this);
		g.setColor(Color.black);
		
		for (int i = 1; i < linesX; i++) {
			int posX = getWidth() * i / linesX;
			g.drawLine(posX, 0, posX, getHeight());					
		}
		
		
		for (int i = 1; i < linesY; i++) {
			int posY = getHeight() * i / linesY;
			g.drawLine(0, posY, getWidth(), posY);					
		}
	}
	
	public void setLinesX(int linesX) {
		this.linesX = linesX;
	}
	
	public void setLinesY(int linesY) {
		this.linesY = linesY;
	}
	
	public void setImg(BufferedImage img) {
		this.img = img;
	}

	public void setWidth(int width) {
		int height = (int) (boardHeight * width / boardWidth);
		setSize(width, height);		
	}

	public void setBoardSize(double d, double e) {
		this.boardWidth = d;
		this.boardHeight = e;
		
	}

	public void setOriginalSize(double d, double e) {
		this.originalWidth = d;
		this.originalHeight = e;		
	}

	public void setShamt(int shiftX, int shiftY) {
		this.shiftX = shiftX;
		this.shiftY = shiftY;
	}

	public void setScale(int scale) {
		this.scale = scale;	
	}
}
